package General.CropPic;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

import android.os.Environment;
import android.util.Log;

/**
 * 文件管理器
 * 
 * @author HuangYi
 * @see QQ：95487710
 **/
public final class HelperMediaFile {

	private static HashMap<String, String> sFileTypeMap = new HashMap<String, String>();

	static {
		addFileType("stl", "application/SLA");
		addFileType("step", "application/STEP");
		addFileType("stp", "application/STEP");
		addFileType("dwg", "application/acad");
		addFileType("ez", "application/andrew-inset");
		addFileType("ccad", "application/clariscad");
		addFileType("drw", "application/drafting");
		addFileType("tsp", "application/dsptype");
		addFileType("dxf", "application/dxf");
		addFileType("xls", "application/excel");
		addFileType("unv", "application/i-deas");
		addFileType("jar", "application/java-archive");
		addFileType("hqx", "application/mac-binhex40");
		addFileType("cpt", "application/mac-compactpro");
		addFileType("pot", "application/vnd.ms-powerpoint");
		addFileType("pps", "application/vnd.ms-powerpoint");
		addFileType("ppt", "application/vnd.ms-powerpoint");
		addFileType("ppz", "application/vnd.ms-powerpoint");
		addFileType("doc", "application/msword");
		addFileType("bin", "application/octet-stream");
		addFileType("class", "application/octet-stream");
		addFileType("dms", "application/octet-stream");
		addFileType("exe", "application/octet-stream");
		addFileType("lha", "application/octet-stream");
		addFileType("lzh", "application/octet-stream");
		addFileType("oda", "application/oda");
		addFileType("ogg", "application/ogg");
		addFileType("ogm", "application/ogg");
		addFileType("pdf", "application/pdf");
		addFileType("pgp", "application/pgp");
		addFileType("ai", "application/postscript");
		addFileType("eps", "application/postscript");
		addFileType("ps", "application/postscript");
		addFileType("prt", "application/pro_eng");
		addFileType("rtf", "application/rtf");
		addFileType("set", "application/set");
		addFileType("smi", "application/smil");
		addFileType("smil", "application/smil");
		addFileType("sol", "application/solids");
		addFileType("vda", "application/vda");
		addFileType("mif", "application/vnd.mif");
		addFileType("xlc", "application/vnd.ms-excel");
		addFileType("xll", "application/vnd.ms-excel");
		addFileType("xlm", "application/vnd.ms-excel");
		addFileType("xls", "application/vnd.ms-excel");
		addFileType("xlw", "application/vnd.ms-excel");
		addFileType("cod", "application/vnd.rim.cod");
		addFileType("arj", "application/x-arj-compressed");
		addFileType("bcpio", "application/x-bcpio");
		addFileType("vcd", "application/x-cdlink");
		addFileType("pgn", "application/x-chess-pgn");
		addFileType("cpio", "application/x-cpio");
		addFileType("csh", "application/x-csh");
		addFileType("deb", "application/x-debian-package");
		addFileType("dcr", "application/x-director");
		addFileType("dir", "application/x-director");
		addFileType("dxr", "application/x-director");
		addFileType("dvi", "application/x-dvi");
		addFileType("pre", "application/x-freelance");
		addFileType("spl", "application/x-futuresplash");
		addFileType("gtar", "application/x-gtar");
		addFileType("gz", "application/x-gunzip");
		addFileType("gz", "application/x-gzip");
		addFileType("hdf", "application/x-hdf");
		addFileType("ipx", "application/x-ipix");
		addFileType("ips", "application/x-ipscript");
		addFileType("js", "application/x-javascript");
		addFileType("skd", "application/x-koan");
		addFileType("skm", "application/x-koan");
		addFileType("skp", "application/x-koan");
		addFileType("skt", "application/x-koan");
		addFileType("latex", "application/x-latex");
		addFileType("lsp", "application/x-lisp");
		addFileType("scm", "application/x-lotusscreencam");
		addFileType("mif", "application/x-mif");
		addFileType("bat", "application/x-msdos-program");
		addFileType("com", "application/x-msdos-program");
		addFileType("exe", "application/x-msdos-program");
		addFileType("cdf", "application/x-netcdf");
		addFileType("nc", "application/x-netcdf");
		addFileType("pl", "application/x-perl");
		addFileType("pm", "application/x-perl");
		addFileType("rar", "application/x-rar-compressed");
		addFileType("sh", "application/x-sh");
		addFileType("shar", "application/x-shar");
		addFileType("swf", "application/x-shockwave-flash");
		addFileType("sit", "application/x-stuffit");
		addFileType("sv4cpio", "application/x-sv4cpio");
		addFileType("sv4crc", "application/x-sv4crc");
		addFileType("tar.gz", "application/x-tar-gz");
		addFileType("tgz", "application/x-tar-gz");
		addFileType("tar", "application/x-tar");
		addFileType("tcl", "application/x-tcl");
		addFileType("tex", "application/x-tex");
		addFileType("texi", "application/x-texinfo");
		addFileType("texinfo", "application/x-texinfo");
		addFileType("man", "application/x-troff-man");
		addFileType("me", "application/x-troff-me");
		addFileType("ms", "application/x-troff-ms");
		addFileType("roff", "application/x-troff");
		addFileType("t", "application/x-troff");
		addFileType("tr", "application/x-troff");
		addFileType("ustar", "application/x-ustar");
		addFileType("src", "application/x-wais-source");
		addFileType("zip", "application/x-zip-compressed");
		addFileType("zip", "application/zip");
		addFileType("tsi", "audio/TSP-audio");
		addFileType("au", "audio/basic");
		addFileType("snd", "audio/basic");
		addFileType("kar", "audio/midi");
		addFileType("mid", "audio/midi");
		addFileType("midi", "audio/midi");
		addFileType("mp2", "audio/mpeg");
		addFileType("mp3", "audio/mpeg");
		addFileType("mpga", "audio/mpeg");
		addFileType("au", "audio/ulaw");
		addFileType("aif", "audio/x-aiff");
		addFileType("aifc", "audio/x-aiff");
		addFileType("aiff", "audio/x-aiff");
		addFileType("m3u", "audio/x-mpegurl");
		addFileType("wax", "audio/x-ms-wax");
		addFileType("wma", "audio/x-ms-wma");
		addFileType("rpm", "audio/x-pn-realaudio-plugin");
		addFileType("ram", "audio/x-pn-realaudio");
		addFileType("rm", "audio/x-pn-realaudio");
		addFileType("ra", "audio/x-realaudio");
		addFileType("wav", "audio/x-wav");
		addFileType("pdb", "chemical/x-pdb");
		addFileType("xyz", "chemical/x-pdb");
		addFileType("ras", "image/cmu-raster");
		addFileType("gif", "image/gif");
		addFileType("ief", "image/ief");
		addFileType("jpe", "image/jpeg");
		addFileType("jpeg", "image/jpeg");
		addFileType("jpg", "image/jpeg");
		addFileType("png", "image/png");
		addFileType("tif tiff", "image/tiff");
		addFileType("tif", "image/tiff");
		addFileType("tiff", "image/tiff");
		addFileType("ras", "image/x-cmu-raster");
		addFileType("pnm", "image/x-portable-anymap");
		addFileType("pbm", "image/x-portable-bitmap");
		addFileType("pgm", "image/x-portable-graymap");
		addFileType("ppm", "image/x-portable-pixmap");
		addFileType("rgb", "image/x-rgb");
		addFileType("xbm", "image/x-xbitmap");
		addFileType("xpm", "image/x-xpixmap");
		addFileType("xwd", "image/x-xwindowdump");
		addFileType("iges", "model/iges");
		addFileType("igs", "model/iges");
		addFileType("mesh", "model/mesh");
		addFileType("msh", "model/mesh");
		addFileType("silo", "model/mesh");
		addFileType("vrml", "model/vrml");
		addFileType("wrl", "model/vrml");
		addFileType("css", "text/css");
		addFileType("htm", "text/html");
		addFileType("html htm", "text/html");
		addFileType("html", "text/html");
		addFileType("asc txt", "text/plain");
		addFileType("asc", "text/plain");
		addFileType("c", "text/plain");
		addFileType("cc", "text/plain");
		addFileType("f90", "text/plain");
		addFileType("f", "text/plain");
		addFileType("h", "text/plain");
		addFileType("hh", "text/plain");
		addFileType("m", "text/plain");
		addFileType("txt", "text/plain");
		addFileType("rtx", "text/richtext");
		addFileType("rtf", "text/rtf");
		addFileType("sgm", "text/sgml");
		addFileType("sgml", "text/sgml");
		addFileType("tsv", "text/tab-separated-values");
		addFileType("jad", "text/vnd.sun.j2me.app-descriptor");
		addFileType("etx", "text/x-setext");
		addFileType("xml", "text/xml");
		addFileType("dl", "video/dl");
		addFileType("fli", "video/fli");
		addFileType("flv", "video/flv");
		addFileType("gl", "video/gl");
		addFileType("mp2", "video/mpeg");
		addFileType("mp4", "video/mp4");
		addFileType("mpe", "video/mpeg");
		addFileType("mpeg", "video/mpeg");
		addFileType("mpg", "video/mpeg");
		addFileType("mov", "video/quicktime");
		addFileType("qt", "video/quicktime");
		addFileType("viv", "video/vnd.vivo");
		addFileType("vivo", "video/vnd.vivo");
		addFileType("fli", "video/x-fli");
		addFileType("asf", "video/x-ms-asf");
		addFileType("asx", "video/x-ms-asx");
		addFileType("wmv", "video/x-ms-wmv");
		addFileType("wmx", "video/x-ms-wmx");
		addFileType("wvx", "video/x-ms-wvx");
		addFileType("avi", "video/x-msvideo");
		addFileType("movie", "video/x-sgi-movie");
		addFileType("mime", "www/mime");
		addFileType("ice", "x-conference/x-cooltalk");
		addFileType("vrm", "x-world/x-vrml");
		addFileType("vrml", "x-world/x-vrml");
	}

	static void addFileType(String extension, String mimeType) {
		sFileTypeMap.put(extension, mimeType);
	}

	public static String getFileType(String path) {
		int lastDot = path.lastIndexOf(".");
		if (lastDot < 0)
			return null;
		return sFileTypeMap.get(path.substring(lastDot + 1).toLowerCase());
	}
	
	public static void setFilepermission(String mFilename){
//		File mSampleFile=new File(mFilename);
//		FileStatus fstatus=new FileStatus();
//		FileUtils.getFileStatus(mSampleFile.getAbsolutePath(), fstatus);
//		FileUtils.setPermissions(mSampleFile.getAbsolutePath(),
//		FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO,fstatus.uid, fstatus.gid);

		if (mFilename == null) return;
		 Log.d("setFilepermission",mFilename);
		 if(mFilename.startsWith( Environment.getExternalStorageDirectory().getPath())){
			 Log.d("setFilepermission","ExternalStorageDirectory:"+Environment.getExternalStorageDirectory().getPath());
			 return;
		 }
		
		try {
			File outputFile=new File(mFilename);
			Class<?> fileUtilsClass;
			fileUtilsClass = Class.forName("android.os.FileUtils");
			Class[] classes=fileUtilsClass.getClasses();
			for(int i=0;i<classes.length;i++)
				Log.d("setFilepermission",classes[i].getName());
			Class<?> fileStatusClass = Class
					.forName("android.os.FileUtils$FileStatus");
			Method getFileStatus = fileUtilsClass.getMethod("getFileStatus",
					String.class, fileStatusClass);
			Object fstatus = fileStatusClass.newInstance();
			getFileStatus.invoke(null, outputFile.getAbsolutePath(), fstatus);
			// FileUtils.getFileStatus(outputFile.getAbsolutePath(), fstatus);
			Method setPermissions = fileUtilsClass.getMethod("setPermissions",
					String.class, int.class, int.class, int.class);
			int S_IRWXU = fileUtilsClass.getField("S_IRWXU").getInt(null);
			int S_IRWXG = fileUtilsClass.getField("S_IRWXG").getInt(null);
			int S_IRWXO = fileUtilsClass.getField("S_IRWXO").getInt(null);
			int fstatusuid = fileStatusClass.getField("uid").getInt(fstatus);
			int fstatusgid = fileStatusClass.getField("gid").getInt(fstatus);
			Log.d("setFilepermission", "S_IRWXU:"+S_IRWXU+"S_IRWXG:"+S_IRWXG+"S_IRWXO:"+S_IRWXO);
			Log.d("setFilepermission", "fstatusuid:"+fstatusuid+"fstatusgid:"+fstatusgid);
			setPermissions.invoke(null, outputFile.getAbsolutePath(), S_IRWXU|S_IRWXG|S_IRWXO, fstatusuid, fstatusgid);
			// FileUtils.setPermissions(outputFile.getAbsolutePath(),
			// FileUtils.S_IRWXU | FileUtils.S_IRWXG |
			// FileUtils.S_IRWXO,fstatus.uid, fstatus.gid);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
